/*
 * Created on 2005-1-17
 */
package com.tss.util;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jedi.FormatDate;
import com.sun.org.apache.regexp.internal.RE;

/**
 * @author DaiRongBang
 *
 * ����request��ع���
 */
public class ParamUtils {
	public ParamUtils() {
	}
	/*private void test() {
		HttpSession session;
		//session.getAttribute()
		//session.removeAttribute("");
		session.setMaxInactiveInterval()
		
	}*/
	public static String getParameter(
		HttpServletRequest request, String name) 
	{
		return getParameter(request, name, false);
		
	}

	public static boolean isFileName(String id) {
		if (id == null || id.trim().equals("")) return true;
		String strEnable = "abcdefghijklmnopqrstuvwxyz_0123456789.";
		for(int i=0;i<id.length();i++) {
			if (strEnable.indexOf(id.substring(i,i+1).toLowerCase()) < 0) return false;
		}
		return true;
	}
	
	public static boolean isRegId(String id) {
		if (id == null || id.trim().equals("")) return true;
		String strEnable = "abcdefghijklmnopqrstuvwxyz_0123456789";
		for(int i=0;i<id.length();i++) {
			if (strEnable.indexOf(id.substring(i,i+1).toLowerCase()) < 0) return false;
		}
		return true;
	}
	
	public static String encode(String param) {
		if (param == null) return "";
		try {
			return new String(param.getBytes("ISO-8859-1"),"GB2312");
		}catch(Exception ex) {
			return param;
		}
	}
	
	public static String getParameter(
		HttpServletRequest request, 
		String name, String defaultStr)
	{
		String param = getParameter(request, name, true);
		if (param == null || param.trim().equals(""))
			param = defaultStr;
		return param;
	}
	
	public static String charRandom(int num) {
		return charRandom(false,num);
	}
	
	public static String charRandom(boolean isUpper,int num) {
		String strLower = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9";
		String strUpper = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9";
		String[] strArray = strLower.split(",");
		if (isUpper)
		{
			strArray=strUpper.split(",");
		}
		StringBuffer r = new StringBuffer();
		for(int i=0;i<num;i++)
		{
			r.append(strArray[new Double(Math.random()*36).intValue()]);
		}
		return r.toString();
	}
	
	public static String getParameter(
		HttpServletRequest request, 
		String name, boolean emptyStringsOK)
	{
		String temp = request.getParameter(name);
		if(temp != null) {
			if(temp.equals("") && !emptyStringsOK)
				return null;
			else
				return encode(temp);
		} else {
			return null;
		}
	}

	public static String[] getParameters(
		HttpServletRequest request, String name)
	{
		if(name == null)
			return new String[0];
		String paramValues[] = request.getParameterValues(name);
		if(paramValues == null || paramValues.length == 0)
			return new String[0];
		List values = new ArrayList(paramValues.length);
		for(int i = 0; i < paramValues.length; i++)
			if(paramValues[i] != null && !"".equals(paramValues[i]))
				values.add(encode(paramValues[i]));
		return (String[])values.toArray(new String[0]);
	}

	public static boolean getBooleanParameter(
		HttpServletRequest request, String name)
	{
		return getBooleanParameter(request, name, false);
	}

	public static boolean getBooleanParameter(
		HttpServletRequest request, String name, 
		boolean defaultVal)
	{
		String temp = request.getParameter(name);
		if("true".equals(temp) || "on".equals(temp))
			return true;
		if("false".equals(temp) || "off".equals(temp))
			return false;
		else
			return defaultVal;
	}

	public static int getIntParameter(
		HttpServletRequest request, String name, int defaultNum)
	{
		String temp = request.getParameter(name);
		if(temp != null && !temp.equals("")) {
			int num = defaultNum;
			try {
				num = Integer.parseInt(temp);
			}
			catch(Exception ignored) { }
			return num;
		} 
		else {
			return defaultNum;
		}
	}

	public static Date getDateParameter(
			HttpServletRequest request, String name, Date defaultNum)
		{
		
		FormatDate formatDate= new FormatDate();
			String temp = request.getParameter(name);
			if(temp != null && !temp.equals("")) {
				Date num = defaultNum;
				try {
					num = formatDate.toDate(temp);
				}
				catch(Exception ignored) { }
				return num;
			} 
			else {
				return defaultNum;
			}
		}
	public static int[] getIntParameters(HttpServletRequest request, String name, int defaultNum)
	{
		String paramValues[] = request.getParameterValues(name);
		if(paramValues == null || paramValues.length == 0)
			return new int[0];
		int values[] = new int[paramValues.length];
		for(int i = 0; i < paramValues.length; i++)
			try {
				values[i] = Integer.parseInt(paramValues[i]);
			}
			catch(Exception e) {
				values[i] = defaultNum;
			}
		return values;
	}

	public static double getDoubleParameter(HttpServletRequest request, String name, double defaultNum)
	{
		String temp = request.getParameter(name);
		if(temp != null && !temp.equals("")) {
			double num = defaultNum;
			try {
				num = Double.parseDouble(temp);
			}
			catch(Exception ignored) { }
			return num;
		} 
		else {
			return defaultNum;
		}
	}

	public static long getLongParameter(HttpServletRequest request, String name, long defaultNum)
	{
		String temp = request.getParameter(name);
		if(temp != null && !temp.equals("")) {
			long num = defaultNum;
			try {
				num = Long.parseLong(temp);
			}
			catch(Exception ignored) { }
			return num;
		} 
		else {
			return defaultNum;
		}
	}

	public static long[] getLongParameters(HttpServletRequest request, String name, long defaultNum)
	{
		String paramValues[] = request.getParameterValues(name);
		if(paramValues == null || paramValues.length == 0)
			return new long[0];
		long values[] = new long[paramValues.length];
		for(int i = 0; i < paramValues.length; i++)
			try {
				values[i] = Long.parseLong(paramValues[i]);
			}
			catch(Exception e) {
				values[i] = defaultNum;
			}
		return values;
	}

	public static String getAttribute(HttpServletRequest request, String name)
	{
		return getAttribute(request, name, false);
	}

	public static String getAttribute(HttpServletRequest request, String name, boolean emptyStringsOK)
	{
		String temp = (String)request.getAttribute(name);
		if(temp != null) {
			if(temp.equals("") && !emptyStringsOK)
				return null;
			else
				return temp;
		} 
		else {
			return null;
		}
	}

	public static boolean getBooleanAttribute(HttpServletRequest request, String name)
	{
		String temp = (String)request.getAttribute(name);
		return temp != null && temp.equals("true");
	}

	public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum)
	{
		String temp = (String)request.getAttribute(name);
		if(temp != null && !temp.equals("")) {
			int num = defaultNum;
			try {
				num = Integer.parseInt(temp);
			}
			catch(Exception ignored) { }
			return num;
		} 
		else {
			return defaultNum;
		}
	}

	public static boolean inArray(Object obj,Object [] arrayObj) {
		if (arrayObj == null || arrayObj.length == 0) return false;
		for(int i=0;i<arrayObj.length;i++) {
			if (arrayObj[i].equals(obj)) return true;
		}
		return false;
	}
	
	public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum)
	{
		String temp = (String)request.getAttribute(name);
		if(temp != null && !temp.equals("")) {
			long num = defaultNum;
			try {
				num = Long.parseLong(temp);
			}
			catch(Exception ignored) { }
			return num;
		} 
		else {
			return defaultNum;
		}
	}
	
	public static void setCookies(HttpServletResponse response,String name, String value)
	{
		try {
			Cookie c = new Cookie(name,java.net.URLEncoder.encode(value,"gb2312"));
			c.setMaxAge(1000000);
			c.setPath("/");
			response.addCookie(c);
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static String getCookies(HttpServletRequest request,String name)
	{
		String cookieVal = "";
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return null;
		for (int i = 0; i < cookies.length; i++) {
			Cookie c = cookies[i];
			if (c.getName().equals(name))
				cookieVal = c.getValue();
		}
		cookieVal=(cookieVal==null||cookieVal.trim().equals(""))?"":cookieVal.trim();
		String ret="";
		try{
			ret = java.net.URLDecoder.decode(cookieVal,"gb2312");
		}
		catch(Exception ex)
		{
			ret="";
		}
		return ret;
	}
	/**
	 * 验证email
	 * @param mail
	 * @return
	 */
	
	public static boolean isEmail(String mail) {
		if (mail.trim().equals("")) return false;
		if (mail.charAt(0) == '.'
			|| mail.charAt(0) == '@'
			|| mail.indexOf("@") == -1 ||
		mail.indexOf(".") == -1 ||
		mail.lastIndexOf("@") == mail.length() -1 ||
		mail.lastIndexOf(".") == mail.length() -1) {
			return false;
		} 
		for(int i=0;i<mail.length();i++) {
			if (mail.charAt(i) > 127) return false;
		}
		return true;
	}
	
	
	/**
	 * 验证电话号码
	 * @param str
	 * @return
	 */
	public static boolean isPhone(String str){
		
		try{
			RE phone =new RE("\\(?\\d\\d\\d\\)? *\\-? *\\d\\d\\d *\\-? *\\d\\d\\d\\d");
			return (phone.match(str));
			
		}catch(Exception ex){
			
			ex.printStackTrace();
			
			return false;
		}
	}
	/**
	 * 验证电话号码
	 * @param str
	 * @return
	 */
	public static boolean isvalidPostalcode(String str){
		
		try{
			RE phone =new RE("\\d\\d\\d\\d\\d(\\-\\d\\d\\d\\d)?");
			return (phone.match(str));
			
		}catch(Exception ex){
			
			ex.printStackTrace();
			return false;
		}
	}
	/**
	 *  验证Double
	 * @param str
	 * @return
	 */
	public static boolean isDouble(String str){
		
		try{
			Double.parseDouble(str);
			return true;
			
		}catch(Exception ex){
			
			//ex.printStackTrace();
			return false;
		}
	}
	/**
	 *  验证null
	 * @param str
	 * @return
	 */
	public static boolean isBlankstring(String str){
		
	
			if((str==null)||(str.trim().equals(""))) return true;
			
			return (str.length()==0);
	
	}
	/**
	 *  验证Int
	 * @param str
	 * @return
	 */
	public static boolean isInt(String str){
		
		try{
			Integer.parseInt(str);
			return true;
			
		}catch(Exception ex){
			
			//ex.printStackTrace();
			return false;
		}
	}
	public static String getRemoteHost(HttpServletRequest request)
	{
		String ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (ip == null || ip.trim().equals("")) ip = request.getRemoteAddr(); 
		return ip;
	}
	
	public static double intMoney(String money) {
		String s = money.indexOf(".") >= 0 ? money + "00" : money + ".00";
		s = s.substring(0,s.indexOf(".")) + s.substring(s.indexOf(".") + 1,s.indexOf(".") + 3);
		return Double.parseDouble(s);
	}
	public static String strMoney(double money) {
		//Double F = new Double(money);
		//return  F.toString();
		return Double.valueOf(money).toString();
	} 
	
	public static String strMoney(long money) {
		return strMoney(money + "");
	}
	
	public static String strMoney(String money) {
		if (money.trim().length() < 3) {
			String temp = "0." + money.trim() + "0000";
			return temp.substring(0,4);
		}
		return money.trim().substring(0,money.trim().length() - 2) + "." + money.trim().substring(money.trim().length() - 2);
	}
	
	public static String formatMoney(String money) {
		if (money.length() < 3) return "0." + money;
		String s = money.substring(0,money.length() - 2);
		String zero = "." + money.substring(money.length() - 2);
		String r ="";
		while (s.length() > 3) {
			r = "," + s.substring(s.length() - 3) + r;
			s = s.substring(0,s.length() - 3);
		}
		return s + r + zero;
	}
}
